package projet.volleystat;

import java.util.List;
import java.util.TreeMap;

import projet.volleystat.automate.VolleyStatistic;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class StatPlayersAdapter extends ArrayAdapter<Player> {
	protected List<Player> players;
	protected VolleyStatistic stats;
	protected DBInfo dbinfo;

	public StatPlayersAdapter(Context context, int ressource,
			List<Player> players) {
		super(context, ressource, players);
		this.players = players;
		dbinfo = new DBInfo(getContext());
		try {
			dbinfo.loadAll();
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		stats = new VolleyStatistic(dbinfo);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		if (v == null) {
			LayoutInflater vi = LayoutInflater.from(getContext());
			v = vi.inflate(R.layout.listitemstats, null);
		}

		Player player = players.get(position);

		if (player != null) {
			TextView stattextname = (TextView) v
					.findViewById(R.id.stattextname);
			TextView stattextfirstname = (TextView) v
					.findViewById(R.id.stattextfirstname);
			TextView nbservetext = (TextView) v.findViewById(R.id.nbservetext);
			TextView nbfaulttext = (TextView) v.findViewById(R.id.nbfaulttext);
			TextView nbpasstext = (TextView) v.findViewById(R.id.nbpasstext);
			TextView nbmarkedtext = (TextView) v
					.findViewById(R.id.nbmarkedtext);
			TextView nbreceptiontext = (TextView) v
					.findViewById(R.id.nbrecepttext);
			TextView nbreplacetext = (TextView) v
					.findViewById(R.id.nbreplacetext);
			TextView avgmarkmarkedtext = (TextView) v
					.findViewById(R.id.avgmarkmarkedtext);
			TextView avgmarkreceptiontext = (TextView) v
					.findViewById(R.id.avgmarkrecepttext);
			TextView avgmarkpasstext = (TextView) v
					.findViewById(R.id.avgmarkpasstext);

			TreeMap<String, Double> statsplayer = new TreeMap<String, Double>();
			try {
				statsplayer = stats.analysisPlayer(player.getId());
			} catch (DatabaseException e) {
				e.printStackTrace();
			}

			Double serve = statsplayer.get("service");
			Double fault = statsplayer.get("faute");
			Double pass = statsplayer.get("passe");
			Double mark = statsplayer.get("point");
			Double reception = statsplayer.get("reception");
			Double replacement = statsplayer.get("remplacement");
			Double scoremark = statsplayer.get("scorePoint");
			Double scorereception = statsplayer.get("scoreReception");
			Double scorepass = statsplayer.get("scorePass");

			if (stattextname != null)
				stattextname.setText(player.getFirstname());
			if (stattextfirstname != null)
				stattextfirstname.setText(player.getName());
			if (nbservetext != null)
				nbservetext.setText(Double.toString(serve));
			if (nbfaulttext != null)
				nbfaulttext.setText(Double.toString(fault));
			if (nbpasstext != null)
				nbpasstext.setText(Double.toString(pass));
			if (nbmarkedtext != null)
				nbmarkedtext.setText(Double.toString(mark));
			if (nbreceptiontext != null)
				nbreceptiontext.setText(Double.toString(reception));
			if (nbreplacetext != null)
				nbreplacetext.setText(Double.toString(replacement));
			if (avgmarkmarkedtext != null)
				avgmarkmarkedtext.setText(Double.toString(scoremark));
			if (avgmarkreceptiontext != null)
				avgmarkreceptiontext.setText(Double.toString(scorereception));
			if (avgmarkpasstext != null)
				avgmarkpasstext.setText(Double.toString(scorepass));
		}

		return v;

	}

}
